package org.example.mall.Service.impl;

import org.example.mall.Mapper.ConsigneeInfoMapper;
import org.example.mall.Mapper.OrderMapper;
import org.example.mall.Mapper.ProductsMapper;
import org.example.mall.Mapper.UsersMapper;
import org.example.mall.Pojo.*;
import org.example.mall.Service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class OrderServiceimpl implements OrderService {
    @Autowired
    private OrderMapper oMapper;
    @Autowired
    private ProductsMapper pMapper;
    @Autowired
    private ConsigneeInfoMapper cMapper;

    @Override
    public List<OrderDetail> getOrderListByUserName(String userName) {
        List<Order> orderList = oMapper.getUserOrderListByUserName(userName);
        List<OrderDetail> orderDetailsList = new java.util.ArrayList<>(Collections.emptyList());
        for(Order order: orderList) {
            Products product = pMapper.findByPId(order.getPId());
            orderDetailsList.add(new OrderDetail(order, product, null));
        }
        return orderDetailsList;
    }

    @Override
    public OrderDetail getOrderDetailByOId(Integer oId) {
        Order order= oMapper.getUserOrderByOId(oId);
        if(order == null) {
            return null;
        }
        Products product = pMapper.findByPId(order.getPId());
        ConsigneeInfo consignee = cMapper.getConsigneeInfoByCId(order.getCId());
        return new OrderDetail(order, product, consignee);
    }

    @Override
    public Integer createOrder(String userName, String pId, Float price, Integer cId) {
        Order order = new Order();
        order.setUserName(userName);
        order.setPId(pId);
        order.setPrice(price);
        order.setCId(cId);
        oMapper.createOrder(order);
        return order.getOId();
    }
    @Override
    public void deleteOrder(Integer oId) {
        oMapper.deleteOrderByOId(oId);
    }
    @Override
    public void deleteOrder(Integer oId, String userName) {
        Order order = oMapper.getUserOrderByOId(oId);
        if(order != null && userName.equals(order.getUserName())) {
            oMapper.deleteOrderByOId(oId);
            if(oMapper.countOrderByCId(order.getCId()) == 0) {
                cMapper.deleteConsigneeInfo(order.getCId());
            }
        }
    }
    @Override
    public void payOrder(Integer oId) {
        oMapper.updateOrderCompleteDate(oId);
        oMapper.updateOrderStatus(oId, 1);
    }

    @Override
    public void completeOrder(Integer oId) {
        oMapper.updateOrderCompleteDate(oId);
        oMapper.updateOrderStatus(oId, 2);
    }
    @Override
    public void cancelOrder(Integer oId) {
        oMapper.updateOrderCompleteDate(oId);
        oMapper.updateOrderStatus(oId, 3);
    }

    @Override
    public void updateAddress(Integer oId, Integer cId) {
        oMapper.updateOrderConsignee(oId, cId);
        oMapper.updateOrderStatus(oId, 2);
    }
}
